package com.yylending.las.common.widgets;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewStub;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.yyfax.common.CLog;
import com.yyfax.common.tools.DensityUtil;
import com.yylending.las.R;

public abstract class BaseDialog extends Dialog {

	@BindView(R.id.dialog_title_tv)
	TextView mDialogTitleTv;
	//@BindView(R.id.dialog_content_tv)
	//TextView mDialogContentTv;
	@BindView(R.id.dialog_cancel_btn)
	TextView mDialogCancelBtn;
	@BindView(R.id.dialog_confirm_btn)
	TextView mDialogConfirmBtn;
	@BindView(R.id.dialog_btn_layout)
	LinearLayout mDialogBtnLayout;
	@BindView(R.id.dialog_btn_divider)
	View mDialogBtnDivider;
	@BindView(R.id.dialog_content_container)
	ViewStub mDialogContentContainer;
	@BindView(R.id.dialog_btn_top_divider)
	View mDialogBtnTopDivider;
	@BindView(R.id.dialog_ll)
	LinearLayout mDialogLl;

	private String mTitleString;
	private String mContentString;
	private String mCancelBtnString;
	private String mConfirmBtnString;

	private int mLeftBtnTextColor = R.color.color_4b7cf9;
	private int mLeftBtnTextSize = 14;
	private int mRightBtnTextColor = R.color.color_4b7cf9;
	private int mRightBtnTextSize = 14;
	private int mBtnHeight = 78;
	private int mWidth = 536;

	private int mContentLayoutId = -1;
	/**
	 * 是否隐藏按钮
	 */
	private boolean mHideButton = false;

	private ButtonClickListener mListener;

	public BaseDialog(Context ctx, String titleString, String contentString, String confirmBtnString,
			ButtonClickListener listener) {
		this(ctx);
		mTitleString = titleString;
		mContentString = contentString;
		mConfirmBtnString = confirmBtnString;
		mListener = listener;
	}

	public BaseDialog(Context ctx, String titleString, String contentString, String confirmBtnString,
			ButtonClickListener listener, boolean cancelAble) {
		this(ctx);
		mTitleString = titleString;
		mContentString = contentString;
		mConfirmBtnString = confirmBtnString;
		mListener = listener;
		setCancelable(cancelAble);
	}

	public BaseDialog(Context ctx, String titleString, String contentString, String confirmBtnString,
			String cancelBtnString, ButtonClickListener listener) {
		this(ctx);
		mTitleString = titleString;
		mContentString = contentString;
		mConfirmBtnString = confirmBtnString;
		mCancelBtnString = cancelBtnString;
		mListener = listener;
	}

	public BaseDialog(Context ctx, String titleString, String contentString, String confirmBtnString,
			String cancelBtnString, ButtonClickListener listener, boolean cancelable) {
		this(ctx);
		mTitleString = titleString;
		mContentString = contentString;
		mConfirmBtnString = confirmBtnString;
		mCancelBtnString = cancelBtnString;
		mListener = listener;
		setCancelable(cancelable);
	}

	public BaseDialog(Context ctx, String titleString, String contentString, String confirmBtnString,
			String cancelBtnString, ButtonClickListener listener, boolean cancelable, int contentLayoutId) {
		this(ctx);
		mTitleString = titleString;
		mContentString = contentString;
		mConfirmBtnString = confirmBtnString;
		mCancelBtnString = cancelBtnString;
		mListener = listener;
		setCancelable(cancelable);
		mContentLayoutId = contentLayoutId;
	}

	public BaseDialog(Context ctx, String titleString, boolean hideButton, int contentLayoutId) {
		this(ctx);
		mTitleString = titleString;
		mHideButton = hideButton;
		setCancelable(true);
		mContentLayoutId = contentLayoutId;
	}

	public BaseDialog(Context ctx) {
		super(ctx, R.style.progress_style);
		mBtnHeight = DensityUtil.dip2px(getContext(), 39);
		mWidth = DensityUtil.dip2px(getContext(), 268);
		CLog.i("CommonDialog", "dialog 构造");
		setCancelable(true);
		setCanceledOnTouchOutside(true);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		CLog.i("CommonDialog", "dialog onCreate");
		setContentView(R.layout.dialog_base_layout);
		ButterKnife.bind(this);
		initView();
	}

	private void initView() {
		if (TextUtils.isEmpty(mTitleString)) {
			mDialogTitleTv.setVisibility(View.GONE);
		} else {
			mDialogTitleTv.setText(mTitleString);
			mDialogTitleTv.setVisibility(View.VISIBLE);
		}
		FrameLayout.LayoutParams dialogLayoutParam = (FrameLayout.LayoutParams) mDialogLl.getLayoutParams();
		dialogLayoutParam.width = mWidth;
		mDialogLl.setLayoutParams(dialogLayoutParam);
		initContentLayout();
		initContent();
		initButton();
	}

	private void initContentLayout() {
		if (mContentLayoutId == -1) {
			mDialogContentContainer.setLayoutResource(R.layout.dialog_content_simple);
		} else {
			mDialogContentContainer.setLayoutResource(mContentLayoutId);
		}
		mDialogContentContainer.inflate();
	}

	private void initContent() {
		if (mContentLayoutId == -1) {
			TextView contentTv = (TextView) findViewById(R.id.dialog_simple_content_tv);
			if (TextUtils.isEmpty(mContentString)) {
				contentTv.setVisibility(View.GONE);
			} else {
				contentTv.setText(mContentString);
				contentTv.setVisibility(View.VISIBLE);
			}
		} else {
			initSpecialContent();
		}
	}

	/**
	 * 初始化自定义布局
	 */
	protected abstract void initSpecialContent();

	private void initButton() {
		if (mHideButton) {
			mDialogBtnTopDivider.setVisibility(View.GONE);
			mDialogBtnLayout.setVisibility(View.GONE);
		} else {
			LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mDialogBtnLayout.getLayoutParams();
			layoutParams.height = mBtnHeight;
			mDialogBtnLayout.setLayoutParams(layoutParams);
			mDialogBtnTopDivider.setVisibility(View.VISIBLE);
			mDialogBtnLayout.setVisibility(View.VISIBLE);
			if (TextUtils.isEmpty(mCancelBtnString)) {
				mDialogBtnDivider.setVisibility(View.GONE);
				mDialogCancelBtn.setVisibility(View.GONE);
				mDialogConfirmBtn.setBackgroundResource(R.drawable.selector_dialog_bottom_corner_round);
			} else {
				mDialogCancelBtn.setText(mCancelBtnString);
				mDialogBtnDivider.setVisibility(View.VISIBLE);
				mDialogCancelBtn.setVisibility(View.VISIBLE);
				mDialogCancelBtn.setBackgroundResource(R.drawable.selector_dialog_left_corner_round);
				mDialogConfirmBtn.setBackgroundResource(R.drawable.selector_dialog_right_corner_round);
			}
			if (TextUtils.isEmpty(mConfirmBtnString)) {
				mDialogConfirmBtn.setText(getContext().getString(R.string.confirm));
			} else {
				mDialogConfirmBtn.setText(mConfirmBtnString);
			}
			mDialogCancelBtn.setTextColor(ContextCompat.getColor(getContext(), mLeftBtnTextColor));
			mDialogCancelBtn.setTextSize(TypedValue.COMPLEX_UNIT_SP, mLeftBtnTextSize);
			mDialogConfirmBtn.setTextColor(ContextCompat.getColor(getContext(), mRightBtnTextColor));
			mDialogConfirmBtn.setTextSize(TypedValue.COMPLEX_UNIT_SP, mRightBtnTextSize);
		}
	}

	public void setLeftBtnTextColor(int colorId) {
		mLeftBtnTextColor = colorId;
	}

	public void setLeftBtnTextSize(int textSizeSp) {
		mLeftBtnTextSize = textSizeSp;
	}

	public void setRightBtnTextColor(int colorId) {
		mRightBtnTextColor = colorId;
	}

	public void setRightBtnTextSize(int textSizeSp) {
		mRightBtnTextSize = textSizeSp;
	}

	public void setBtnHeight(int height) {
		mBtnHeight = height;
	}

	public void setDialogWidth(int width){
		mWidth = width;
	}

	@OnClick({R.id.dialog_cancel_btn, R.id.dialog_confirm_btn})
	public void onViewClicked(View view) {
		switch (view.getId()) {
			case R.id.dialog_cancel_btn:
				if (null != mListener) {
					mListener.onCancel(this);
				} else {
					dismiss();
				}
				break;
			case R.id.dialog_confirm_btn:
				if (null != mListener) {
					mListener.onConfirm(this);
				} else {
					dismiss();
				}
				break;
		}
	}

	public interface ButtonClickListener {
		void onCancel(BaseDialog dialog);

		void onConfirm(BaseDialog dialog);
	}
}
